package com.commerce.service.impl;

import com.commerce.authority.Authority;
import com.commerce.common.Reminder;
import com.commerce.constant.UserType;
import com.commerce.mapping.AdminMapping;
import com.commerce.mapping.MenuMapping;
import com.commerce.mapping.RoleMapping;
import com.commerce.model.Admin;
import com.commerce.model.Menu;
import com.commerce.page.AdminPage;
import com.commerce.page.Page;
import com.commerce.service.AdminService;
import com.commerce.util.Base64Img;
import com.commerce.util.MDS;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

/**
 * @author wyj
 * @Title:
 * @Description:
 */
@Service
public class AdminServiceImpl implements AdminService{

    @Autowired
    private AdminMapping adminMapping;

    @Autowired
    private MenuMapping menuMapping;

    @Autowired
    private RoleMapping roleMapping;

    @Value("${web.upload-path}")
    private String webuploadpath;

    /**
     * 分辨查询
     *
     * @param adminPage
     * @return
     */
    @Override
    public Page selectByPage(AdminPage adminPage) {
        PageHelper.startPage(adminPage.getPage(),adminPage.getLimit());
        adminPage.setList(adminMapping.selectPage(adminPage));
        return adminPage;
    }

    /**
     * 登录查询
     *
     * @param admin
     * @param HttpSession
     * @return
     */
    @Override
    public String login(Admin admin, HttpSession HttpSession) {
        admin = adminMapping.selectByNamePassword(admin);
        if(admin == null)
            return Reminder.accountpassworderror.getName();
        if( !admin.isOpen() )
            return Reminder.adminnoopenerror.getName();
        List<Menu> menuList = null;
        if(admin.getRole_ids() != null && !admin.getRole_ids().equals("")){
            String menu_ids = roleMapping.selectMenuids(admin.getRole_ids());
            menuList = menuMapping.selectMenuIdsMenu(menu_ids);
            admin.authoritysUtil(menuList);
        }else{
            menuList =menuMapping.selectList();
            admin.setAuthoritys("ALL");
        }
        HttpSession.setAttribute(UserType.ADMINUSET.name(),admin);
        HttpSession.setAttribute("userMenu",menuList);
        return Reminder.success.getName();
    }

    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @Override
    public String deleteById(int id) {
        return adminMapping.deleteById(id)>0?Reminder.success.name():Reminder.deleteerror.name();
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @Override
    public Admin selectById(int id) {
        return adminMapping.selectById(id);
    }

    /**
     * 查询账号
     *
     * @param name
     * @return
     */
    @Override
    public String selectByAccount(String name) {
        return adminMapping.selectByAccountOrId(name,0)>0?Reminder.accountrepetitionerror.name():Reminder.success.name();
    }

    /**
     * 新增管理员
     *
     * @param admin
     * @param request
     * @return
     */
    @Override
    public String insert(Admin admin, HttpServletRequest request) {
        if(adminMapping.selectByAccountOrId(admin.getAccount(),0)>0)
            return Reminder.accountrepetitionerror.name();
        admin.rold_idsUtil();
        if(admin.getRole_ids() == null || admin.getRole_ids().trim().equals(""))
            return Reminder.rolenoselecterror.name();
        admin.setImgurl(Base64Img.uploadImg(admin.getImgurl(),webuploadpath));
        if(admin.getImgurl() == null){
            return Reminder.uploadimgerror.name();
        }
        admin.setPassword(MDS.encryptBasedDes(admin.getAccount()+admin.getPassword()));
        return adminMapping.insert(admin)>0?Reminder.success.name():Reminder.saveerror.name();
    }

    /**
     * 修改管理员
     *
     * @param admin
     * @param request
     * @return
     */
    @Override
    public String update(Admin admin, HttpServletRequest request) {
        if(adminMapping.selectByAccountOrId(admin.getAccount(),admin.getId())>0)
            return Reminder.accountrepetitionerror.name();
        admin.rold_idsUtil();
        if( (admin.getRole_ids() == null || admin.getRole_ids().trim().equals("")) && admin.getId()!=1)
            return Reminder.rolenoselecterror.name();
        if(admin.getImgurl().length()>100){
            admin.setImgurl(Base64Img.uploadImg(admin.getImgurl(),webuploadpath));
            if(admin.getImgurl() == null){
                return Reminder.uploadimgerror.name();
            }
        }
        admin.setPassword(MDS.encryptBasedDes(admin.getAccount()+admin.getPassword()));
        return adminMapping.update(admin)>0?Reminder.success.name():Reminder.updateerror.name();
    }

    /**
     * 开启或关闭
     *
     * @param id
     * @return
     */
    @Override
    public String updateByOpen(int id) {
        return adminMapping.updateByOpen(id)>0?Reminder.success.name():Reminder.updateerror.name();
    }

    /**
     * 修改密码
     *
     * @param pwd
     * @param pwdnew
     * @param session
     * @return
     */
    @Override
    public String updatePwd(String password ,String pwd, String pwdnew, HttpSession session) {
        if(password == null || password.trim().equals(""))
            return Reminder.pwdnoempty.getName();
        if(pwd == null || pwd.trim().equals(""))
            return Reminder.pwdnoempty.getName();
        if( !pwd.equals(pwdnew) )
            return Reminder.pwdnoequals.getName();
        Admin admin = (Admin)session.getAttribute(UserType.ADMINUSET.name());
        if( !admin.getPassword().equals(MDS.encryptBasedDes(admin.getAccount()+password)))
            return Reminder.rawpwdnoequals.getName();

        admin.setPassword(MDS.encryptBasedDes(admin.getAccount()+pwd));
        if(adminMapping.updateByPwd(admin)==0)
            return Reminder.updateerror.getName();
        session.invalidate();
        session=null;
        return Reminder.success.getName();
    }
}
